#include "wx/wxprec.h"
#if wxUSE_RADIOBOX

#if wxUSE_TOOLTIPS
#include "wx/tooltip.h"
#endif // wxUSE_TOOLTIPS

#if wxUSE_HELP
#include "wx/cshelp.h"
#endif

void wxRadioBoxBase::SetMajorDim( unsigned int majorDim, long style ) {
  wxCHECK_RET( majorDim != 0, _T( "major radiobox dimension can't be 0" ) );
  m_majorDim = majorDim;
  int minorDim = ( GetCount() + m_majorDim - 1 ) / m_majorDim;
  if( style & wxRA_SPECIFY_COLS ) {
    m_numCols = majorDim;
    m_numRows = minorDim;
  } else { // wxRA_SPECIFY_ROWS
    m_numCols = minorDim;
    m_numRows = majorDim;
  }
}

int wxRadioBoxBase::GetNextItem( int item, wxDirection dir, long style ) const {
  const int itemStart = item;
  int count = GetCount(),
      numCols = GetColumnCount(),
      numRows = GetRowCount();
  bool horz = ( style & wxRA_SPECIFY_COLS ) != 0;
  do {
    switch( dir ) {
      case wxUP:
        if( horz ) {
          item -= numCols;
        } else { // vertical layout
          if( !item-- ) {
            item = count - 1;
          }
        }
        break;
      case wxLEFT:
        if( horz ) {
          if( !item-- ) {
            item = count - 1;
          }
        } else // vertical layout
        { item -= numRows; }
        break;
      case wxDOWN:
        if( horz ) {
          item += numCols;
        } else { // vertical layout
          if( ++item == count ) {
            item = 0;
          }
        }
        break;
      case wxRIGHT:
        if( horz ) {
          if( ++item == count ) {
            item = 0;
          }
        } else // vertical layout
        { item += numRows; }
        break;
      default:
        wxFAIL_MSG( _T( "unexpected wxDirection value" ) );
        return wxNOT_FOUND;
    }
    if( item < 0 ) {
      item += count;
      if( item % ( horz ? numCols : numRows ) ) {
        item--;
      } else
      { item = count - 1; }
    } else if( item >= count ) {
      item -= count;
      if( ( item + 1 ) % ( horz ? numCols : numRows ) ) {
        item++;
      } else
      { item = 0; }
    }
    wxASSERT_MSG( item < count && item >= 0,
                  _T( "logic error in wxRadioBox::GetNextItem()" ) );
  } while( !( IsItemShown( item ) && IsItemEnabled( item ) ) && item != itemStart );
  return item;
}

#if wxUSE_TOOLTIPS

void wxRadioBoxBase::SetItemToolTip( unsigned int item, const wxString& text ) {
  wxASSERT_MSG( item < GetCount(), _T( "Invalid item index" ) );
  if( !m_itemsTooltips ) {
    m_itemsTooltips = new wxToolTipArray;
    m_itemsTooltips->resize( GetCount() );
  }
  wxToolTip *tooltip = ( *m_itemsTooltips )[item];
  bool changed = true;
  if( text.empty() ) {
    if( tooltip ) {
      delete tooltip;
      tooltip = NULL;
    } else // nothing to do
    { changed = false; }
  } else { // non empty tooltip text
    if( tooltip ) {
      tooltip->SetTip( text );
      changed = false;
    } else { // no tooltip yet
      // create the new one
      tooltip = new wxToolTip( text );
    }
  }
  if( changed ) {
    ( *m_itemsTooltips )[item] = tooltip;
    DoSetItemToolTip( item, tooltip );
  }
}

void
wxRadioBoxBase::DoSetItemToolTip( unsigned int ( item ), wxToolTip * ( tooltip ) ) {
}

#endif

wxRadioBoxBase::~wxRadioBoxBase() {
  #if wxUSE_TOOLTIPS
  if( m_itemsTooltips ) {
    const size_t n = m_itemsTooltips->size();
    for( size_t i = 0; i < n; i++ ) {
      delete( *m_itemsTooltips )[i];
    }
    delete m_itemsTooltips;
  }
  #endif
}

#if wxUSE_HELP

void wxRadioBoxBase::SetItemHelpText( unsigned int n, const wxString& helpText ) {
  wxCHECK_RET( n < GetCount(), _T( "Invalid item index" ) );
  if( m_itemsHelpTexts.empty() ) {
    m_itemsHelpTexts.Add( wxEmptyString, GetCount() );
  }
  m_itemsHelpTexts[n] = helpText;
}

wxString wxRadioBoxBase::GetItemHelpText( unsigned int n ) const {
  wxCHECK_MSG( n < GetCount(), wxEmptyString, _T( "Invalid item index" ) );
  return m_itemsHelpTexts.empty() ? wxString() : m_itemsHelpTexts[n];
}

wxString wxRadioBoxBase::DoGetHelpTextAtPoint( const wxWindow *derived,
    const wxPoint& pt,
    wxHelpEvent::Origin origin ) const {
  const int item = origin == wxHelpEvent::Origin_HelpButton ? GetItemFromPoint( pt ) : GetSelection();
  if( item != wxNOT_FOUND ) {
    wxString text = GetItemHelpText( wx_static_cast( unsigned int, item ) );
    if( !text.empty() ) {
      return text;
    }
  }
  return derived->wxWindowBase::GetHelpTextAtPoint( pt, origin );
}

#endif


#endif
